package com.hospital.register.core.repository.impl;

import com.hospital.register.constants.BaseEntityConstants;
import com.hospital.register.constants.DepartmentConstants;
import com.hospital.register.core.repository.DepartmentRepository;
import com.hospital.register.dto.core.api.DepartmentPageDto;
import com.hospital.register.model.core.Department;
import com.hospital.register.vo.core.api.DepartmentPageVo;
import com.mongodb.client.result.UpdateResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

@Repository
public class DepartmentRepositoryImpl implements DepartmentRepository {

    /**
     * 访问 Mongodb
     */
    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public boolean exists(String hoscode, String depcode) {
        Criteria criteria = Criteria.where(DepartmentConstants.HOSCODE).is(hoscode)//医院编号
                .and(DepartmentConstants.DEPCODE).is(depcode)//科室编号
                .and(BaseEntityConstants.FIELD_IS_DELETED).is(BaseEntityConstants.NOT_DELETED);//未删除

        Query query = Query.query(criteria);
        return this.mongoTemplate.count(query, Department.class) == 1L;
    }

    @Override
    public boolean insert(Department department) {
        return this.mongoTemplate.insert(department) != null;
    }

    @Override
    public boolean update(Department department) {
        Criteria criteria = Criteria.where(DepartmentConstants.HOSCODE).is(department.getHoscode())//医院编号
                .and(DepartmentConstants.DEPCODE).and(department.getDepcode())//科室编号
                .and(BaseEntityConstants.FIELD_IS_DELETED).is(BaseEntityConstants.NOT_DELETED);//未删除
        Query query = Query.query(criteria);

        Update update = Update.update(DepartmentConstants.DEPNAME, department.getDepname())//科室名称
                .set(DepartmentConstants.INTRO, department.getIntro())//科室描述
                .set(DepartmentConstants.BIGCODE, department.getBigcode())//大科室编号
                .set(DepartmentConstants.BIGNAME, department.getBigname())//大科室名称
                .set(BaseEntityConstants.UPDATE_TIME, department.getUpdateTime());//更新时间

        UpdateResult updateResult = this.mongoTemplate.updateFirst(query, update, Department.class);
        return updateResult.wasAcknowledged();
    }

    @Override
    public Page<DepartmentPageVo> findPage(DepartmentPageDto dto) {
        Criteria criteria = Criteria.where(DepartmentConstants.HOSCODE).is(dto.getHoscode())//医院编号
                .and(BaseEntityConstants.FIELD_IS_DELETED).is(BaseEntityConstants.NOT_DELETED);//未删除
        Query query = Query.query(criteria);
        long total = this.mongoTemplate.count(query, DepartmentConstants.COLLECTION_NAME);

        Pageable pageable = PageRequest.of(dto.getPage() - 1, dto.getLimit());
        query.with(pageable);
        List<DepartmentPageVo> vos = this.mongoTemplate.find(query, DepartmentPageVo.class, DepartmentConstants.COLLECTION_NAME);

        Page<DepartmentPageVo> page = new PageImpl<>(vos, pageable, total);
        return page;
    }

    @Override
    public boolean remove(String hoscode, String depcode) {
        Criteria criteria = Criteria.where(DepartmentConstants.HOSCODE).is(hoscode)//医院编号
                .and(DepartmentConstants.DEPCODE).is(depcode)//科室编号
                .and(BaseEntityConstants.FIELD_IS_DELETED).is(BaseEntityConstants.NOT_DELETED);//未删除
        Query query = Query.query(criteria);

        Update update = Update.update(BaseEntityConstants.FIELD_IS_DELETED, BaseEntityConstants.DELETED)//标记删除
                .set(BaseEntityConstants.UPDATE_TIME, new Date());//更新时间

        UpdateResult updateResult = this.mongoTemplate.updateFirst(query, update, DepartmentConstants.COLLECTION_NAME);
        return updateResult.wasAcknowledged();
    }

    @Override
    public List<Department> findByHoscode(String hoscode) {
        Criteria criteria = Criteria.where(DepartmentConstants.HOSCODE).is(hoscode)//医院编号
                .and(BaseEntityConstants.FIELD_IS_DELETED).is(BaseEntityConstants.NOT_DELETED);//未删除的

        Query query = Query.query(criteria);
        return this.mongoTemplate.find(query, Department.class);
    }

    @Override
    public Department findOne(String hoscode, String depcode) {
        Criteria criteria = Criteria.where(DepartmentConstants.HOSCODE).is(hoscode)//医院编号
                .and(DepartmentConstants.DEPCODE).is(depcode)//科室编号
                .and(BaseEntityConstants.FIELD_IS_DELETED).is(BaseEntityConstants.NOT_DELETED);//未删除

        Query query = Query.query(criteria);
        return this.mongoTemplate.findOne(query, Department.class);
    }

}
